package com.zongzi.stock.module;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.zongzi.stock.R;
import com.zongzi.stock.bean.SearchResultBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Dell on 2017/3/28.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity mActivity;
    private SearchResultBean mSearchResultBean;

    public SearchAdapter(Activity activity, SearchResultBean searchResultBean) {
        mActivity = activity;
        mSearchResultBean = searchResultBean;
        Logger.d("进入SearchAdapter");
    }


    //自定义监听事件
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mActivity)
                .inflate(R.layout.adapter_search, parent, false);
        return new MyViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        ((MyViewHolder) holder).mTvNum.
                setText(mSearchResultBean.data.stock_data.get(position).f_symbol);
        ((MyViewHolder) holder).mTvName.
                setText(mSearchResultBean.data.stock_data.get(position).f_symbolName);
        Logger.d("size:" + mSearchResultBean.data.stock_data.size()
                + "\n" + "num:" + mSearchResultBean.data.stock_data.get(position).f_symbol
                + "\n" + "name:" + mSearchResultBean.data.stock_data.get(position).f_symbolName);
        // 如果设置了回调，则设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(v, position);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                return false;
            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_num)
        TextView mTvNum;
        @BindView(R.id.tv_name)
        TextView mTvName;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        return mSearchResultBean.data.stock_data.size();
    }
}
